package com.tchy.cloud.controller;

import com.tchy.cloud.entity.ACustomerEntity;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.common.service.CommonService;
import org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by xiaoji on 2017/2/23.
 * 用户管理
 */
@Controller
@RequestMapping("/aCustomerController")
public class ACustomerController {
    private static final Logger logger = Logger.getLogger(ACustomerController.class.getName());
    @Autowired
    private CommonService commonService;

    /**
     * 普通用户跳转
     *
     * @return
     */
    @RequestMapping(params = "view")
    public ModelAndView view() {
        return new ModelAndView("clound/customer/view");
    }


    /**
     * 获取咨询师列表
     * @param aCustomerEntity 普通用户对象
     * @param request
     * @param response
     * @param dataGrid
     */
    @RequestMapping(params = "datagrid")
    public void datagrid(ACustomerEntity aCustomerEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
        CriteriaQuery cq = new CriteriaQuery(ACustomerEntity.class, dataGrid);
        cq.addOrder("customerId", SortDirection.desc);
        cq.add();
        // 查询条件组装
        HqlGenerateUtil.installHql(cq, aCustomerEntity, request.getParameterMap());
        commonService.getDataGridReturn(cq, true);
        TagUtil.datagrid(response, dataGrid);
    }

    /**
     * 审核
     * @param aCustomerEntity 用户对象
     * @param req
     * @return
     */
    @RequestMapping(params = "disable")
    @ResponseBody
    public AjaxJson disable(ACustomerEntity aCustomerEntity, HttpServletRequest req) {
        AjaxJson j = new AjaxJson();
        try{
            ACustomerEntity aCustomerEntity2 = commonService.getEntity(ACustomerEntity.class, aCustomerEntity.getCustomerId());
            aCustomerEntity2.setStatus(aCustomerEntity.getStatus());
            commonService.save(aCustomerEntity2);
            j.setMsg("操作成功");
        }catch (Exception e){
            logger.error(e);
            j.setSuccess(false);
            j.setMsg("操作失败");
        }
        return j;
    }
}
